﻿using System.Linq;
using CoreLibrary;
using CoreLibrary.Email;
using WebService.Config;

namespace WebService.Email {

    public class EmailManager {

        public static void SendEmail(IEmailTemplate template) {
            if (!AppSettings.IsProduction) {
                 return;
            }

            var recipients = AppSettings.SupportEmails.Select(email => new EmailAddress(email)).ToList();
            if (CollectionUtil.IsEmpty(recipients)) {
                return;
            }

            foreach (var emailAddress in recipients) {
                var builder = new EmailRequestBuilder();
                builder.SetHtmlContent(template.GenerateBody());
                builder.SetTextContent(string.Empty);
                builder.SetReplyTo(template.ReplyTo);
                builder.SetFrom(template.Sender.Name, template.Sender.Email);
                builder.AddTo(emailAddress);
                builder.SetSubject(template.Subject);
                Services.Locate<IEmailService>().SendEmail(builder.Build());
            }
   
        }

        public static void SendEmail(string subject, string text) {
            if (!AppSettings.IsProduction) {
                return;
            }

            var recipients = AppSettings.SupportEmails.Select(email => new EmailAddress(email)).ToList();
            if (CollectionUtil.IsEmpty(recipients)) {
                return;
            }
            foreach (var emailAddress in recipients) {
                var builder = new EmailRequestBuilder();
                builder.SetHtmlContent("<strong>"+text+"</strong>");
                builder.SetTextContent(text);
                builder.SetReplyTo(new EmailAddress("mingyi.frank@gmail.com"));
                builder.SetFrom("进销存管理系统邮件", "mingyi.frank@gmail.com");
                builder.AddTo(emailAddress);
                builder.SetSubject(subject);
                Services.Locate<IEmailService>().SendEmail(builder.Build());
            }
        }

    }
}